package com.yiqi.liebang.feature.home.view;


import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.baiiu.filter.ClassifyBo;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.entity.enums.OrderType;
import com.yiqi.liebang.feature.home.contract.ClassifyContract;
import com.yiqi.liebang.feature.home.dependencies.classsify.ClassifyPresenterModule;
import com.yiqi.liebang.feature.home.dependencies.classsify.DaggerClassifyComponent;
import com.yiqi.liebang.feature.home.view.adapter.ChoiceAdpter;
import com.yiqi.liebang.feature.order.model.MessageEvent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class ExpertFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, ClassifyContract.View,
        OnRefreshListener {
    @BindView(R.id.rv_expert)
    RecyclerView mRvExpert;
    @BindView(R.id.btn_expert_ask)
    TextView mBtnExpertAsk;
    Unbinder unbinder;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;
    private boolean isSelect = false;

    private double mAskPrize;
    private String mAskContent;
    private SearchFriendBo.UserFriendBean mUserUid;

    @Inject
    ClassifyContract.Presenter mPresenter;

    private ClassifyBo mTitle;
    private EmptyView mEmptyView;
    ChoiceAdpter questionAdapter = null;

    public static ExpertFragment getInstance(ClassifyBo title) {
        ExpertFragment sf = new ExpertFragment();
        sf.mTitle = title;
        return sf;
    }


    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_expert;
    }

    @Override
    protected void initInjector() {
        DaggerClassifyComponent.builder().classifyPresenterModule(new ClassifyPresenterModule(this)).build().inject(this);
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    protected void initView() {
        initAdapter();
        mAskPrize = getActivity().getIntent().getDoubleExtra("ask_prize", 0.0);
        mAskContent = getActivity().getIntent().getStringExtra("ask_content");
        String userUid = getActivity().getIntent().getStringExtra("user_id");
        if (!TextUtils.isEmpty(userUid)){
            mUserUid=new SearchFriendBo.UserFriendBean();
            mUserUid.setUserUid(userUid);
            mPresenter.getUserLabelByType(mUserUid.getUserUid());
        }
        mBtnExpertAsk.setText("花费" + mAskPrize + "元，向TA提问");
    }

    @Override
    protected void initData() {

        if (mTitle != null) {
            mPresenter.getClassifyExpert(mTitle.getId(), 1, 10);
            if (mTitle.getId().equals("0")){
                mSmartRefesh.autoRefresh();
            }
        }
    }

    @Override
    protected void initEvent() {
        RxBus.getDefault().register(this);
    }

    private void initAdapter() {
        mSmartRefesh.setOnRefreshListener(this);
        mSmartRefesh.setEnableLoadMore(false);
        mEmptyView = new EmptyView(mRvExpert);
        mRvExpert.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvExpert.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        questionAdapter = new ChoiceAdpter();
        questionAdapter.bindToRecyclerView(mRvExpert);
        questionAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        questionAdapter.changeState(position);
        mUserUid = questionAdapter.getData().get(position);
    }


    @OnClick(R.id.btn_expert_ask)
    public void onViewClicked() {
        if (mUserUid == null) {
            T.showShort("请选择行家？");
            return;
        }

        mPresenter.getUserLabelByType(mUserUid.getUserUid());
    }

    @Override
    public void showUserSuccess(String classifyBos) {
        Intent intent = new Intent(getActivity(), PayActivity.class);
        intent.putExtra("ask_prize", mAskPrize);
        intent.putExtra("ask_userid", mUserUid.getUserUid());
        intent.putExtra("ask_content", mAskContent);
        intent.putExtra("ask_lables", classifyBos);
        intent.putExtra("order_type", OrderType.WENDA.getIndex());
        startActivity(intent);
        getActivity().finish();
    }

    @Override
    public void showUserError(String classifyBos) {
        showMsg(classifyBos);
    }

    @Override
    public void showSuccess(List<ClassifyBo> classifyBos) {

    }

    @Override
    public void showExpert(List<SearchFriendBo.UserFriendBean> classifyBos) {
        mSmartRefesh.finishRefresh();
        questionAdapter.setNewData(classifyBos);

    }

    @Override
    public void showEmpty() {
        mSmartRefesh.finishRefresh();
        questionAdapter.setEmptyView(mEmptyView.getEmptyView("暂无此类型行家"));
        questionAdapter.setNewData(null);
    }

    @Override
    public void showError(String ex) {
        mSmartRefesh.finishRefresh();
        questionAdapter.setEmptyView(mEmptyView.getErrorView(ex));
        questionAdapter.setNewData(null);
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (mTitle != null) {
            mPresenter.getClassifyExpert(mTitle.getId(), 1, 10);
        }
    }

    /**
     * EventBus 3.0 回调
     *
     * @param obj
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(MessageEvent obj) {
        mUserUid = obj.getMessage();
        isSelect = true;
        if (mUserUid != null) {
            List<SearchFriendBo.UserFriendBean> userFriendBeans = new ArrayList<>();
            userFriendBeans.add(mUserUid);
            questionAdapter.setNewData(userFriendBeans);
            questionAdapter.changeState(0);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }
}
